package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserRelation;
import com.ruoyi.system.service.IUserRelationService;

/**
 * 用户关联Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/relation")
public class UserRelationController extends BaseController
{
    @Autowired
    private IUserRelationService userRelationService;

    /**
     * 查询用户关联列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody UserRelation userRelation)
    {
        startPage();
        List<UserRelation> list = userRelationService.selectUserRelationList(userRelation);
        return getDataTable(list);
    }

    /**
     * 获取用户关联详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userRelationService.selectUserRelationById(id));
    }

    /**
     * 新增用户关联
     */
    @Log(title = "用户关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserRelation userRelation)
    {
        return toAjax(userRelationService.insertUserRelation(userRelation));
    }

    /**
     * 修改用户关联
     */
    @Log(title = "用户关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody UserRelation userRelation)
    {
        return toAjax(userRelationService.updateUserRelation(userRelation));
    }

    /**
     * 删除用户关联
     */
    @Log(title = "用户关联", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody String ids)
    {
        return toAjax(userRelationService.deleteUserRelationByIds(ids.split(",")));
    }

    /**
     * 导出用户关联列表
     */
    @Log(title = "用户关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody UserRelation userRelation)
    {
        List<UserRelation> list = userRelationService.selectUserRelationList(userRelation);
        ExcelUtil<UserRelation> util = new ExcelUtil<UserRelation>(UserRelation.class);
        return util.exportExcel(list, "relation");
    }

    /**
     * 根据老人用户ID查询关联的家属列表
     */
    @GetMapping("/elder/{elderUserId}/families")
    public AjaxResult getFamiliesByElder(@PathVariable("elderUserId") Long elderUserId)
    {
        List<UserRelation> list = userRelationService.selectUserRelationByElderUserId(elderUserId);
        return AjaxResult.success(list);
    }

    /**
     * 根据家属用户ID查询关联的老人列表
     */
    @GetMapping("/family/{familyUserId}/elders")
    public AjaxResult getEldersByFamily(@PathVariable("familyUserId") Long familyUserId)
    {
        List<UserRelation> list = userRelationService.selectUserRelationByFamilyUserId(familyUserId);
        return AjaxResult.success(list);
    }
} 